﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class EditUnitTrustHeaderModel
    {
        public UnitTrustHeaderScreenData ScreenData { get; set; }
        public UnitTrustHeader Header { get; set; }
        public IList<UnitTrustTransaction> Transactions 
        { 
            set 
            {
                IList<UnitTrustTransaction> data = value;
                if(data != null && data.Count > 0)
                {
                    foreach (var item in data)
                    {
                        TransactionAdaters.Add(new UnitTrustTransactionAdapter(item, Header));
                    }
                }
            }
        }
        public IList<UnitTrustTransactionAdapter> TransactionAdaters { get; set; }
        
        public EditUnitTrustHeaderModel()
        {
            Init();
        }
        void Init()
        {
            if (null == Header)
                Header = new UnitTrustHeader();
            if (TransactionAdaters == null)
                TransactionAdaters = new List<UnitTrustTransactionAdapter>();

            var defaultItem = WebUtil.SelectListSelectDefault();
            CompanyList = new BindingList<SelectListItem>() { defaultItem };
            InvolvedPartyTypeNameList = new BindingList<SelectListItem>() { defaultItem };
            RelatedOrNonRelatedList = new BindingList<SelectListItem>() { defaultItem };
            TypeList = new BindingList<SelectListItem>() { defaultItem };
            GroupOfTypeList = new BindingList<SelectListItem>() { defaultItem };
            AssetManagementCompanyList = new BindingList<SelectListItem>() { defaultItem };
            ListedStatusList = new BindingList<SelectListItem>() { defaultItem };
            //if (string.IsNullOrWhiteSpace(SelectedListedStatus))
            //    SelectedListedStatus = "LISTED";
        }
        public static EditUnitTrustHeaderModel BuildModel(UnitTrustHeaderScreenData data)
        {
            var model = new EditUnitTrustHeaderModel {ScreenData = data};
            if (data.HeaderDTO != null)
            {
                if (data.HeaderDTO.Header != null)
                    model.Header = data.HeaderDTO.Header;
                if (data.HeaderDTO.Transactions != null)
                    model.Transactions = data.HeaderDTO.Transactions;
            }
            if (data.GroupOfTypes != null && data.GroupOfTypes.Any())
            {
                foreach (var selectListItem in data.GroupOfTypes)
                {
                    model.GroupOfTypeList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.Key, Value = selectListItem.ValueEN });
                }
            }
            if (data.RelatedOrNonRelateds != null && data.RelatedOrNonRelateds.Any())
            {
                foreach (var selectListItem in data.RelatedOrNonRelateds)
                {
                    model.RelatedOrNonRelatedList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.Key, Value = selectListItem.ValueEN });
                }
            }
            if (data.Types != null && data.Types.Any())
            {
                foreach (var selectListItem in data.Types)
                {
                    model.TypeList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.Key, Value = selectListItem.ValueEN });
                }
            }
            if (data.Companies != null && data.Companies.Any())
            {
                foreach (var selectListItem in data.Companies)
                {
                    model.CompanyList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.CompanyNameTh, Value = selectListItem.CompanyId.ToString() });
                }
            }
            if (data.ListStatuses != null && data.ListStatuses.Any())
            {
                foreach (var selectListItem in data.ListStatuses)
                {
                    model.ListedStatusList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.ListStatusName, Value = selectListItem.ListStatusName });
                }
            }
            if (data.AssetManagementCompanies != null && data.AssetManagementCompanies.Any())
            {
                foreach (var selectListItem in data.AssetManagementCompanies)
                {
                    model.AssetManagementCompanyList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.AssetManagementCompanyName, Value = selectListItem.AssetManagementCompanyId.ToString() });
                }
            }
            if (data.InvolvedPartyTypes != null && data.InvolvedPartyTypes.Any())
            {
                foreach (var selectListItem in data.InvolvedPartyTypes)
                {
                    model.InvolvedPartyTypeNameList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.InvolvedPartyTypeName, Value = selectListItem.InvolvedPartyTypeId.ToString() });
                }
                if (model.Header != null)
                {
                    var a = model.ScreenData.InvolvedPartyTypes.SingleOrDefault(x => x.InvolvedPartyTypeId == model.Header.InvolvedPartyTypeID);
                    if (a != null)
                    {
                        model.InvolvedPartyTypeReferance = a.InvolvedPartyTypeReference;
                        model.InvolvedPartyTypeNo = a.InvolvedPartyTypeCode;
                    }
                }
            }

            return model;
        }

        #region dropdown list

        [Required(ErrorMessage = "โปรดระบุ")]
        public int? SelectedAssetManagementCompany
        {
            get { return Header.AssetMgmtID; }
            set { Header.AssetMgmtID = value; }
        }

        public IList<SelectListItem> AssetManagementCompanyList { get; set; }

        [Required(ErrorMessage = "โปรดระบุ")]
        public string SelectedListedStatus
        {
            get { return Header.ListedStatus; }
            set { Header.ListedStatus = value; }
        }

        public IList<SelectListItem> ListedStatusList { get; set; }

        [Required(ErrorMessage = "โปรดระบุ")]
        public int? SelectedCompany
        {
            get { return Header.UnitTrustCompanyID; }
            set { Header.UnitTrustCompanyID = value; }
        }
        public IList<SelectListItem> CompanyList { get; set; }

        public int? SelectedInvolvedPartyType
        {
            get { return Header.InvolvedPartyTypeID; }
            set
            {
                Header.InvolvedPartyTypeID = value;
            }
        }
        public IList<SelectListItem> InvolvedPartyTypeNameList { get; set; }


        //Related / Non Related
        public string SelectedRelatedOrNonRelated
        {
            get { return Header.Related; }
            set { Header.Related = value; }
        }
        public IList<SelectListItem> RelatedOrNonRelatedList { get; set; }


        //Type
        public int? SelectedType
        {
            get { return Header.UnitTrustTypeID; }
            set { Header.UnitTrustTypeID = value; }
        }
        public IList<SelectListItem> TypeList { get; set; }

        public string SelectedGroupOfType
        {
            get { return Header.UnitTrustTypeGroup; }
            set { Header.UnitTrustTypeGroup = value; }
        }
        public IList<SelectListItem> GroupOfTypeList { get; set; }

        #endregion

        #region adapter
        public int HeaderID
        {
            get
            {
                return Header.ID;
            }

            set
            {
                Header.ID = value;
            }
        }
        [Required(ErrorMessage = "โปรดระบุ")]
        //[StringLength(200, ErrorMessage = "ระบุได้ไม่เกิน 200 ตัวอักษร")]
        public string FundName
        {
            get { return Header.FundName; }
            set { Header.FundName = value; }
        }
        //[StringLength(200, ErrorMessage = "ระบุได้ไม่เกิน 200 ตัวอักษร")]
        [Required(ErrorMessage = "โปรดระบุ")]
        public string FundNameEn
        {
            get { return Header.FundNameEn; }
            set { Header.FundNameEn = value; }
        }
        
        [Required(ErrorMessage = "โปรดระบุ")]
        public string Symbol
        {
            get { return Header.Symbol; }
            set { Header.Symbol = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? BidOrNavePrice
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(SelectedListedStatus) && SelectedListedStatus.ToUpper() == "NON LISTED")
                {
                    return Header.Nav;
                }
                else
                {
                    return Header.BidPrice;
                }
            }
            set
            {
                if (!string.IsNullOrWhiteSpace(SelectedListedStatus) && SelectedListedStatus.ToUpper() == "NON LISTED")
                {
                    Header.Nav = value;
                }
                else
                {
                    Header.BidPrice = value;
                }
            }
        }

        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        public DateTime? BidOrNaveDate
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(SelectedListedStatus) && SelectedListedStatus.ToUpper().Trim() == "NON LISTED")
                {
                    return Header.NavDate;
                }
                else
                {
                    return Header.BidPriceDate;
                }
            }
            set
            {
                if (!string.IsNullOrWhiteSpace(SelectedListedStatus) && SelectedListedStatus.ToUpper().Trim() == "NON LISTED")
                {
                    Header.NavDate = value;
                }
                else
                {
                    Header.BidPriceDate = value;
                }
            }
        }

        public String InvolvedPartyTypeReferance { get; set; }
        public String InvolvedPartyTypeNo { get; set; }

        private bool _isTierISelected;
        public bool IsTierISelected
        {
            get { return _isTierISelected; }
            set
            {
                _isTierISelected = value;
                //if (value)
                //{
                //    Header.LiquidityTierIPercent = "Tier I";
                //}
            }
        }

        private bool _isTierIiSelected;
        public bool IsTierIISelected
        {
            get { return _isTierIiSelected; }
            set
            {
                _isTierIiSelected = value;
                //if (value)
                //{
                //    Header.LiquidityName = "Tier II";
                //}
            }
        }

        private bool _isLowLiquiditySelected;
        public bool IsLowLiquiditySelected
        {
            get { return _isLowLiquiditySelected; }
            set
            {
                _isLowLiquiditySelected = value;
                //if (value)
                //{
                //    Header.LiquidityName = "Low Liquidity";
                //}
            }
        }


        #endregion

        public String SaveBtn { get; set; }
        public String CancelBtn { get; set; }
        public String EditBtn { get; set; }
        public bool UpdateMode { get; set; }
    }

}